package com.java.gui;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class FocusEventTest extends JFrame {
	private JLabel userLabel,emailLabel,checkLabel;
	private JTextField userText,emailText;
	public FocusEventTest(){
		super("FocusEventTest!!");
		userLabel=new JLabel("用户名：");
		emailLabel=new JLabel("E-mail:");
		checkLabel=new JLabel("    ");
		userText=new JTextField(10);
		emailText=new JTextField(15);
		this.setLayout(new FlowLayout());
		add(userLabel);
		add(userText);
		add(checkLabel);
		add(emailLabel);
		add(emailText);
	}
	public void showMe(){
		addEventHandler();
		this.setSize(400, 300);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	public void addEventHandler(){
		userText.addFocusListener(new FocusAdapter(){
			public void focusLost(FocusEvent e) {
				super.focusLost(e);
				String str=userText.getText();
				char firstChar=str.charAt(0);
				if(str.length()>6&&str.length()<8&&(firstChar>'A'&&firstChar<'Z'||firstChar>'a'&&firstChar<'z')){
					checkLabel.setText("用户名合法！");
				}else{
					checkLabel.setText("用户名不合法！");
				}
			}
			
		});

	}
	public static void main(String[] args){
		new FocusEventTest().showMe();
	}
}
